﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Tsanie.OsuDevice.Components;
using System.IO;

namespace Tsanie.OsuDevice.Utils {
    class LogUtil {
        private static StreamWriter _logWriter = null;
        static LogUtil() {
            _logWriter = new StreamWriter(Config.c_AppPath + @"\\Tsanie.OsuDevice.log", true, Encoding.UTF8);
        }
        public static void Error(Exception ex, Action<Exception> exFunc) {
            if (Config.LogError)
                WriteError(ex, 0);
            if (exFunc != null)
                exFunc(ex);
        }
        private static void WriteError(Exception ex, int indent) {
            if (ex == null)
                return;
            if (indent > 0) {
                _logWriter.Write('└');
                _logWriter.Write(new string('─', indent));
            }
            _logWriter.WriteLine("--------------------------------------------------");
            _logWriter.WriteLine("[{0}] - {1}: {2}", DateTime.Now.ToString("MM-dd HH:mm:ss"), ex.GetType().FullName, ex.Message);
            _logWriter.WriteLine("--------------------------------------------------");
            _logWriter.WriteLine(ex.StackTrace);
            _logWriter.WriteLine();
            _logWriter.Flush();
            if (ex.InnerException != null)
                WriteError(ex.InnerException, indent + 1);
        }
    }

    class TaskbarExt {
        public static void SetState(ITaskbarList3 taskbar, MainForm form, TBPFLAG state) {
            if (taskbar != null)
                taskbar.SetProgressState(form.Handle, state);
            bool change = form.toolProgress.Visible;
            switch (state) {
                case TBPFLAG.TBPF_INDETERMINATE:
                    form.toolProgress.Style = ProgressBarStyle.Marquee;
                    form.toolProgress.Visible = true;
                    change ^= true;
                    break;
                case TBPFLAG.TBPF_NOPROGRESS:
                    form.toolProgress.Visible = false;
                    change ^= false;
                    break;
                case TBPFLAG.TBPF_NORMAL:
                case TBPFLAG.TBPF_PAUSED:
                case TBPFLAG.TBPF_ERROR:
                    form.toolProgress.Style = ProgressBarStyle.Blocks;
                    form.toolProgress.Visible = true;
                    change ^= true;
                    break;
            }
            if (change)
                form.MainForm_Resize(null, null);
        }
        public static void SetValue(ITaskbarList3 taskbar, MainForm form, int completed, int total) {
            if (taskbar != null)
                taskbar.SetProgressValue(form.Handle, (ulong)completed, (ulong)total);
            if (form.toolProgress.Maximum != total)
                form.toolProgress.Maximum = total;
            if (completed <= total && form.toolProgress.Value != completed)
                form.toolProgress.Value = completed;
        }
    }

    class Util {
        public static void ThreadSafeRun(Control c, MethodInvoker m) {
            try {
                if (c != null && c.InvokeRequired)
                    c.Invoke(m);
                else
                    m();
            } catch { }
        }
        public static void ThreadSafeBeginRun(Control c, MethodInvoker m) {
            try {
                if (c != null && c.InvokeRequired)
                    c.BeginInvoke(m);
                else
                    m();
            } catch { }
        }
    }
}
